package com.abc.recycling.service.impl;

import com.abc.recycling.domain.SysConfig;
import com.abc.recycling.mapper.SysConfigMapper;
import com.abc.recycling.service.SysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SysConfigServiceImpl implements SysConfigService {

    private final SysConfigMapper configMapper;
    
    @Override
    @Transactional
    public SysConfig createConfig(SysConfig config) {
        // 检查键名是否存在
        if (isKeyExists(config.getConfigKey(), null)) {
            throw new RuntimeException("配置键名已存在");
        }
        
        // 设置初始状态
        config.setIsSystem(0);
        config.setStatus(1);
        config.setCreateTime(LocalDateTime.now());
        
        configMapper.insert(config);
        return config;
    }
    
    @Override
    @Transactional
    public SysConfig updateConfig(SysConfig config) {
        // 检查配置是否存在
        SysConfig existConfig = getConfig(config.getId());
        if (existConfig == null) {
            throw new RuntimeException("配置不存在");
        }
        
        // 检查是否为系统内置配置
        if (existConfig.getIsSystem() == 1) {
            throw new RuntimeException("系统内置配置不允许修改");
        }
        
        // 检查键名是否存在
        if (isKeyExists(config.getConfigKey(), config.getId())) {
            throw new RuntimeException("配置键名已存在");
        }
        
        config.setUpdateTime(LocalDateTime.now());
        configMapper.update(config);
        return config;
    }
    
    @Override
    public SysConfig getConfig(Long id) {
        return configMapper.findById(id);
    }
    
    @Override
    public SysConfig getConfigByKey(String key) {
        return configMapper.findByKey(key);
    }
    
    @Override
    public List<SysConfig> getAllConfigs() {
        return configMapper.findAll();
    }
    
    @Override
    public List<SysConfig> searchConfigs(String name, String configKey, String type, Integer status) {
        return configMapper.search(name, configKey, type, status);
    }
    
    @Override
    @Transactional
    public boolean updateConfigStatus(Long id, Integer status) {
        // 检查配置是否存在
        SysConfig config = getConfig(id);
        if (config == null) {
            throw new RuntimeException("配置不存在");
        }
        
        // 检查是否为系统内置配置
        if (config.getIsSystem() == 1) {
            throw new RuntimeException("系统内置配置不允许修改状态");
        }
        
        return configMapper.updateStatus(id, status, LocalDateTime.now()) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteConfig(Long id) {
        // 检查配置是否存在
        SysConfig config = getConfig(id);
        if (config == null) {
            throw new RuntimeException("配置不存在");
        }
        
        // 检查是否为系统内置配置
        if (config.getIsSystem() == 1) {
            throw new RuntimeException("系统内置配置不允许删除");
        }
        
        return configMapper.deleteById(id) > 0;
    }
    
    @Override
    public boolean isKeyExists(String configKey, Long excludeId) {
        return configMapper.checkKeyExists(configKey, excludeId) > 0;
    }
    
    @Override
    public String getConfigValue(String key, String defaultValue) {
        SysConfig config = getConfigByKey(key);
        return config != null ? config.getConfigValue() : defaultValue;
    }
    
    @Override
    public Integer getNumberValue(String key, Integer defaultValue) {
        String value = getConfigValue(key, null);
        if (value == null) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    @Override
    public Boolean getBooleanValue(String key, Boolean defaultValue) {
        String value = getConfigValue(key, null);
        if (value == null) {
            return defaultValue;
        }
        return Boolean.parseBoolean(value);
    }
} 